//
//  NewsCollectionView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/9/30.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "NewsCollectionView.h"
#import "NewsCollectionViewCell.h"
#import "NetEaseDataService.h"
#import "NewsViewController.h"
#import "TopScrollView.h"

@implementation NewsCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        self.delegate = self;
        self.dataSource = self;
        
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor clearColor];
        
        [self registerClass:[NewsCollectionViewCell class] forCellWithReuseIdentifier:kNewsCollectionViewCellId];
    }
    
    return self;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 7;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NewsCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kNewsCollectionViewCellId forIndexPath:indexPath];
    
    return cell;
}

//cell将要显示时，网络请求
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NewsCollectionViewCell* newsCell = (NewsCollectionViewCell*)cell;
    
    NSInteger row = indexPath.row;
    
    switch (row) {
        case 0:                             //头条
        {[NetEaseDataService newsHeadNewsDataRequestFrom:0 size:140 block:^(id result) {
            if ([result isKindOfClass:[NSError class]]) {
                NSLog(@"新闻页 头条 数据请求失败 %@", result);
                return;
            }
//            NSLog(@"%@", [Tools getJsonStrFromObj:result]);
            NSArray* array = [result objectForKey:@"T1348647853363"];
            if (array.count) {
                newsCell.newsModelArray = array;
            }
        }];}
            break;
        case 1:                             //NBA
        {[NetEaseDataService newsNBANewsDataRequestFrom:0 size:20 block:^(id result) {
            if ([result isKindOfClass:[NSError class]]) {
                NSLog(@"新闻页 NBA 数据请求失败 %@", result);
                return;
            }
//            NSLog(@"%@", [Tools getJsonStrFromObj:result]);
            NSArray* array = [result objectForKey:@"T1348649145984"];
            if (array.count) {
                newsCell.newsModelArray = array;
            }
        }];}
            break;
        case 2:                             //娱乐
        {[NetEaseDataService newsEntertainmentNewsDataRequestFrom:0 size:20 block:^(id result) {
            if ([result isKindOfClass:[NSError class]]) {
                NSLog(@"新闻页 娱乐 数据请求失败 %@", result);
                return;
            }
            NSArray* array = [result objectForKey:@"T1348648517839"];
            if (array.count) {
                newsCell.newsModelArray = array;
            }
        }];}
            break;
        case 3:                             //时尚
        {[NetEaseDataService newsFashionNewsDataRequestFrom:0 size:20 block:^(id result) {
            if ([result isKindOfClass:[NSError class]]) {
                NSLog(@"新闻页 时尚 数据请求失败 %@", result);
                return;
            }
//            NSLog(@"%@", [Tools getJsonStrFromObj:result]);
            NSArray* array = [result objectForKey:@"T1348650593803"];
            if (array.count) {
                newsCell.newsModelArray = array;
            }
        }];}
            break;
        case 4:                             //汽车
        {[NetEaseDataService newsCarNewsDataRequestFrom:0 size:20 block:^(id result) {
            if ([result isKindOfClass:[NSError class]]) {
                NSLog(@"新闻页 汽车 数据请求失败 %@", result);
                return;
            }
//            NSLog(@"%@", [Tools getJsonStrFromObj:result]);
            NSArray* array = [result objectForKey:@"list"];
            if (array.count) {
                newsCell.newsModelArray = array;
            }
        }];}
            break;
        case 5:                             //房产
        {[NetEaseDataService newsHouseNewsDataRequestFrom:0 size:20 block:^(id result) {
            if ([result isKindOfClass:[NSError class]]) {
                NSLog(@"新闻页 房产 数据请求失败 %@", result);
                return;
            }
//            NSLog(@"%@", [Tools getJsonStrFromObj:result]);
            NSArray* array = [result objectForKey:@"全国"];
            if (array.count) {
                newsCell.newsModelArray = array;
            }
        }];}
            break;
        case 6:                             //段子
        {[NetEaseDataService newsDuanziNewsDataRequestSize:20 block:^(id result) {
            if ([result isKindOfClass:[NSError class]]) {
                NSLog(@"新闻页 段子 数据请求失败 %@", result);
                return;
            }
//            NSLog(@"%@", [Tools getJsonStrFromObj:result]);
            NSArray* array = [result objectForKey:@"段子"];
            if (array.count) {
                newsCell.newsModelArray = array;
            }
        }];}
            break;
        default:
            break;
    }
}

//cell滑动结束时.        实现btn跟着cell走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //计算当前index
    if ([scrollView isKindOfClass:[NewsCollectionView class]]) {
        NSInteger index = scrollView.contentOffset.x/kWidth;
        
        //通过响应者链,找到newsVC
        NewsViewController* newsVC = [Tools className:@"NewsViewController" fromResponder:self.nextResponder];
        if (newsVC) {
            UIView* topView = [newsVC.view viewWithTag:10086];
            if ([topView isKindOfClass:[TopScrollView class]]) {
                TopScrollView* topScrollView = (TopScrollView*)topView;
                
                [topScrollView setCurrentIndex:index];
            }
        }
    }
}


@end
